<template>
  <global-header :user="userInfo" />
  <loader v-if="isLoading" text="加载中..." />
  <!-- <message type="error" :message="error.message" v-if="error.status" /> -->
  <router-view></router-view>
  <footer class="text-center py-4 text-secondary bg-light mt-6">
    <small>
      <ul class="list-inline mb-0">
        <li class="list-inline-item">© 2021 专栏</li>
        <li class="list-inline-item">课程</li>
        <li class="list-inline-item">文档</li>
        <li class="list-inline-item">联系</li>
        <li class="list-inline-item">更多</li>
      </ul>
    </small>
  </footer>
</template>

<script lang="ts">
import { computed, defineComponent, onMounted, watch } from "vue";
import "bootstrap/dist/css/bootstrap.min.css";
// import ColumnList, { ColumnProps } from "./components/ColumnList.vue";
import GlobalHeader from "./components/GlobalHeader.vue";
import { useStore } from "vuex";
import { GlobalDataProps } from "./utils/interface";
import Loader from "./components/Loader.vue";
// import Message from "./components/Message.vue";
import axios from "axios";
import createMessage from "./utils/createMessage";
export default defineComponent({
  name: "App",
  components: { GlobalHeader, Loader },
  setup() {
    const store = useStore<GlobalDataProps>();
    const currentUser = computed(() => store.state.user);
    // 拿到token
    const token = computed(() => store.state.token);
    const error = computed(() => store.state.error);
    onMounted(() => {
      if (!currentUser.value.isLogin && token.value) {
        axios.defaults.headers.common[
          "Authorization"
        ] = `Bearer ${token.value}`;
        store.dispatch("fetchCurrentUser");
      }
    });
    // watch第一个参数为真，执行第二个
    watch(
      () => error.value.status,
      () => {
        const { status, message } = error.value;
        if (status && message) {
          createMessage(message, "error");
        }
      }
    );
    // 记得响应式呀
    const isLoading = computed(() => store.state.loading);
    return {
      userInfo: currentUser,
      isLoading,
      error,
    };
  },
});
</script>

<style>
</style>
